<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('game');
    }
    
    // get recomended 3+
    public function getRecomendedOver($arrayOfGames,$recMin)
    {
    	$arrayRecomended= array();
    	foreach ($arrayOfGames as $item) {
    		if ($item->getOver()>$recMin){
    			array_push($arrayRecomended, $item);
    		}
    	}
    	return $arrayRecomended;
    }
    
    // get recomended 0-2
    public function getRecomendedUnder($arrayOfGames,$recMin)
    {
    	$arrayRecomended= array();
    	foreach ($arrayOfGames as $item) {
    		if ($item->getUnder()>$recMin){
    			array_push($arrayRecomended, $item);
    		}
    	}
    	return $arrayRecomended;
    }
    
    
    
    // return array of leagues ( groped )
    public function getUniqueLeagues($arrayOfGames)
    {
    
    	$leagues=array();
    	foreach ($arrayOfGames as $item)
    	{
    
    
    		if (!(in_array($item->getLeague(), $leagues))){
    			array_push($leagues, $item->getLeague());
    		}
    
    	}
    	return $leagues;
    }
    // filter only games that have no result
    public function filterNotFinished($arrayOfGames)
    {
    	$arrayHelp= array();
    	foreach ($arrayOfGames as $item)
    	{
    
    		if ($item->getResult()==' - '){
    			array_push($arrayHelp, $item);
    		}
    
    	}
    	return $arrayHelp;
    
    }
    
    // returns top games with percent > $recMin 
    public function getRecomended($arrayOfGames,$recMin)
    {
    	$arrayRecomended= array();
    	foreach ($arrayOfGames as $item) {
    		if ($item->getPercentFix1()>$recMin){
    			array_push($arrayRecomended, $item);
    		}
    		if ($item->getPercentFix2()>$recMin){
    			array_push($arrayRecomended, $item);
    		}
    		if ($item->getPercentFixX()>$recMin){
    			array_push($arrayRecomended, $item);
    		}
    	}
    	return $arrayRecomended;
    }    
    
    // filter games par last date
    public function filterLastDateGames($arrayOfGames)
    {
    	$arrayFiltered= array();
    	$dateOfLast=$arrayOfGames[count($arrayOfGames)-1]->getDateFromPage();
    	
    	foreach ($arrayOfGames as $item) {
    		if ($item->getDateFromPage()==$dateOfLast){
    			array_push($arrayFiltered, $item);
    		}    		
    	}
    	return $arrayFiltered;
    }
    
    // index function
    public function index(){
        $arrayOfGames = $this->game->getAllGames();        

        /*foreach ($arrayOfGames as $gameTO){
        	echo '<pre>';
        	echo $gameTO->toString();
        	echo '</pre>';
        }*/
           
            $arrayOfGames = $this->filterNotFinished($arrayOfGames);
            $data['games']=$arrayOfGames;
            $data['leagues']=$this->getUniqueLeagues($arrayOfGames);
            $data['recomended']=$this->getRecomended($arrayOfGames,75);
            $data['recomendedOver']=$this->getRecomendedOver($arrayOfGames,79);
            $data['recomendedUnder']=$this->getRecomendedUnder($arrayOfGames,75);
            $this->load->helper('url');
            $this->load->view('predictions',$data);
          
            
        
    }
    
    public function about(){
        $data['title']="ABOUT";
        $this->load->helper('url');
        $this->load->view('about',$data);
    }
}